use dataset_county, clear

gen l4demvotes = l4.demvotes
gen l4repvotes = l4.repvotes

gen B = .
gen Q = .
gen E = .
	
forvalues i = 10(1)20 {

	preserve

		*** make counterfactual county-level vote share ***

		gen demvotescf = demvotes
		gen repvotescf = repvotes

		drop if mi(pctcollege)
		drop if mi(demvoteshare)

		keep if year == 2016
		 _pctile pctcollege [aw=pop] if year == 2016, percentiles(5(5)95)
		local e = r(r`i')
		if `i' != 21 {
			replace demvotescf = l4demvotes if pctcollege >= r(r`i')
			replace repvotescf = l4repvotes if pctcollege >= r(r`i')
		}
		
		*** aggregate up to state level ***

		keep if year >= 1960 & !mi(stateabbr)
		collapse ev (sum) demvotes repvotes demvotescf repvotescf, by(stateabbr year)

		*** remake dem percent of vote ***

		gen demvoteshare = (demvotes) / (demvotes + repvotes) * 100
		gen demvotesharecf = (demvotescf) / (demvotescf + repvotescf) * 100

		*** fix 2016 odd cases ***

		replace demvoteshare = 42 if stateabbr=="AK" & year == 2016
		replace demvotesharecf = 42 if stateabbr=="AK" & year == 2016

		*** make dem and rep ev ***

		gen demev = .
		replace demev = ev if demvoteshare > 50 & !mi(demvoteshare)
		replace demev = 0 if demvoteshare < 50 & !mi(demvoteshare)
		gen demevcf = .
		replace demevcf = ev if demvotesharecf > 50 & !mi(demvotesharecf)
		replace demevcf = 0 if demvotesharecf < 50 & !mi(demvotesharecf)

		*** fix 2016 odd cases ***

		replace demev = demev - 1 if year== 2016 & statea=="ME" // ME
		replace demevcf = demevcf - 1 if year== 2016 & statea=="ME" // ME
		replace demev = demev - 4 if year == 2016 & statea=="WA" // faithless WA
		replace demevcf = demevcf - 4 if year == 2016 & statea=="WA" // faithless WA
		replace demev = demev - 1 if year == 2016 & statea=="HI" // faithless HI
		replace demevcf = demevcf - 1 if year == 2016 & statea=="HI" // faithless HI

		foreach x in demev demevcf {
			replace `x' = 3 if statea=="DC" & year == 2016
		}

		*** aggregate by year ***

		collapse (sum) demev demevcf, by(year)

		gen threshold = .
		replace threshold = 266 if year == 1952 | year == 1956
		replace threshold = 269 if year == 1960
		replace threshold = 270 if year > 1960

		replace demev = demev - threshold
		replace demevcf = demevcf - threshold

		gen demevcflab = string(demevcf ) 
		replace demevcflab = "+" + demevcflab if demevcf > 0
		replace demevcflab = demevcflab + "*" if sign(demevcf)!=sign(demev)

		su demevcf
		local b = r(mean)
		local q = `i'
		
	restore
	
	replace B = `b' if _n == `i' + 1
	replace Q = `q' if _n == `i' + 1
	replace E = `e' if _n == `i' + 1
	
}

keep if !mi(B)
keep B Q E
replace Q = Q*5

gen mylab = ""
replace mylab = "{&larr}Actual result" if Q == 0
replace mylab = "If counties {&ge} 50th percentile reverted{&rarr}" if Q == 50

drop if Q == 100

#delimit;

gr tw
	(sc B Q if Q == 0, mfcol(black) col(black) msym(O) mlab(mylab)
		mlabpos(3) mlabsize(medlarge))
	(sc B Q if Q != 0, mfcol(white) col(black) msym(O) mlab(mylab)
		mlabpos(9) mlabsize(medlarge))
	, 
		yline(0, lpat(dash) lcol(black))
		xtitle("Percentile of college education")
		xlab(50(5)95)
		ytitle("Clinton's electoral college margin")
		ylab(-70(20)70, angle(horiz))
		plotregion(style(none))
		legend(off)
		xscale(rev)
		title("High education counties")
		;

#delimit cr

gr export "figure 8b.pdf", replace
